package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.mongodb.DBObject;

@SuppressWarnings("serial")
public class CheckLinkSpecialServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setContentType("text/html");
		HttpSession session = request.getSession();
		DBObject dbObject = (DBObject) session.getAttribute("users");
		if(dbObject==null)
		{
			try {
				response.getWriter().println("<script>window.location='http://hatforrent.com/login'</script>");
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		response.setCharacterEncoding("utf-8");

		String sitemap = request.getParameter("sitemap");
		String regtitle = request.getParameter("regtitle");
		String regcontent = request.getParameter("regcontent");
		String regremove = request.getParameter("regremove");
		String sumtitlebefore = request.getParameter("sumtitlebefore");
		String sumtitleafter = request.getParameter("sumtitleafter");
		String language = request.getParameter("language");
		String translate = request.getParameter("translate");
		String rewriter_title = request.getParameter("rewriter_title");
		
		String link=sitemap;
		
		ArrayList<String> site = Utils.getStachSite();
		
		
		String strContent="";
		String ti = "";
		String charset="UTF-8";
		Document doc = null;
		if(!link.equals(""))
		{
//			if(!"yes".equals(translate))
//			{
				if(site.contains(Utils.fomatDmain(sitemap)))
				{
					doc = Jsoup.parse(Utils.GetHtml("http://getstackcontent.appspot.com/stack?link="+link, charset));
				} else {
					doc = Jsoup.parse(Utils.GetHtml(link, charset));
				}
				
				int index = doc.select("meta").toString().toLowerCase()
						.indexOf("charset=");
				if (index != -1) {
					charset = doc.select("meta").toString().substring(index + 8)
							.replaceAll("[^\\'\\\"0-9a-zA-Z\\-\\_]+[\\w\\W]+", "")
							.replaceAll("[\\'\\\"]+", "");
				}
				//
				if(site.contains(Utils.fomatDmain(sitemap)))
				{
					doc = Jsoup.parse(Utils.GetHtml("http://getstackcontent.appspot.com/stack?link="+link, charset));
				} else {
					doc = Jsoup.parse(Utils.GetHtml(link, charset));
				}
//			} else {
//				doc = Utils.Translate(link, language);
//			}
			ti = doc.select(regtitle).text();
			
//			if("yes".equals(rewriter_title))
//			{
//				ti = Utils.Spin(ti);
//			}
			
			doc = Jsoup.parse(doc.select(regcontent).html());
			String[] remove = regremove.split(" ");
			doc.select("script").remove();
			if (remove.length > 0) {
				for (int i = 0; i < remove.length; i++) {
					try {
						if ((remove[i] != null) && (!remove[i].equals("")))
						{
							String[] reg = remove[i].split("\\|"); 
							
							if(reg.length >=2)
							{
								if(reg[1].equals("last"))
								{
									doc.select(reg[0].replaceAll(">", " ")).last().remove();
								} else {
									doc.select(reg[0].replaceAll(">", " ")).first().remove();
								}
							} else {
								doc.select(remove[i].replaceAll(">", " ")).remove();
							}
						}
					} catch (Exception localException1) {
					}
				}
			}
			
			strContent = doc.html();
		}
		strContent = strContent.replaceAll("&amp;", "&");
		try {
			Elements img = doc.select("img");
			for(int i=0;i<img.size();i++)
			{
				String im= img.eq(i).attr("src");
				if(!"".equals(im))
				{
					String imageUrl = Utils.fomatLink(im, sitemap);
					
					strContent = strContent.replace(im, imageUrl);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		ti = sumtitlebefore+" "+ti+" "+sumtitleafter;
		request.setAttribute("str", link);
		request.setAttribute("title", ti.trim());
		request.setAttribute("content", Utils.cleanContent(strContent));
		
		try {
			request.getRequestDispatcher("/check_link_special.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
